/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package reloj.DAO;


import java.util.List;
import javax.persistence.EntityManager;


/**
 *
 * @author USUARIO TERAN
 */

public abstract class DAOAbstract <T> {

    private T instancia;
    private EntityManager entityManager;

    public DAOAbstract(T t) {
        instancia = t;
        entityManager = FuenteDatos.getEntityManager();
    }

    /**
     * @return the instancia
     */
    public T getInstancia() {
        return instancia;
    }

    /**
     * @param instancia the instancia to set
     */
    public void setInstancia(T instancia) {
        this.instancia = instancia;
    }

    /**
     * @return the entityManager
     */
    public EntityManager getEntityManager() {
        return entityManager;
    }

    /**
     * @param entityManager the entityManager to set
     */
    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }


//----------------------------- OPERACIONES BASICAS ---------------------------------------------------------------------    ----

    // AGREGAR UN OBJERTO A LA BD

    public void persist() throws Exception{

        this.entityManager.getTransaction().begin();

        try{
            this.entityManager.persist(instancia);
            this.entityManager.getTransaction().commit();
        }catch(Exception e){
            System.out.println(e);
            if(this.entityManager.getTransaction().isActive()){
                this.entityManager.getTransaction().rollback();
                throw e;
            }            
        }
       
    }//fin void persist

//----------------------------------------------------------------------------------------------------------------------------------------

    // ACTUALIZAR UN OBJERTO EN LA BD

    public void update() throws Exception{

        this.entityManager.getTransaction().begin();

        try{
            instancia = this.entityManager.merge(instancia);
            this.entityManager.getTransaction().commit();
        }catch(Exception e){
            System.out.println(e+" Actualizar");
            if(this.entityManager.getTransaction().isActive()){
                this.entityManager.getTransaction().rollback();
                throw e;
            }            
        }

    }//fin update

//-----------------------------------------------------------------------------------------------------------------------------------

    // ELIMINAR UN OBJERTO EN LA BD

    public void remove() throws Exception{

        this.entityManager.getTransaction().begin();

        try{
            //instancia = findById(instancia.getId());
            this.entityManager.remove(instancia);
            this.entityManager.getTransaction().commit();
        }catch(Exception e){
            System.out.println(e+ " Remover");
            if(this.entityManager.getTransaction().isActive()){
                this.entityManager.getTransaction().rollback();
                throw e;
            }            
        }
                
    }//fin remove

//-------------------------------------------------------------------------------------------------------------------------------------

    public T findById (Integer id){

        return (T) this.entityManager.find(this.instancia.getClass(), id);

    }//fin findById

//--------------------------------------------------------------------------------------------------------------------------------------------

    public abstract List <T> buscarTodos();

//----------------------------------------------------------------------------------------------------------------------------------------


}//fin class DAOabstract
